import jpype
from jpype import *

Lamp_num_getter = jpype.JPackage('com.fonda.protocol.sdk.f2.codec.LampNumGetter')

jars_path = [
    "/Users/starlin/PycharmProjects/sdk/jar/commons-codec-1.15.jar",
    "/Users/starlin/PycharmProjects/sdk/jar/commons-lang3-3.12.0.jar",
    "/Users/starlin/PycharmProjects/sdk/jar/slf4j-api-1.7.36.jar",
    "/Users/starlin/PycharmProjects/sdk/jar/logback-classic-1.2.12.jar",
    "/Users/starlin/PycharmProjects/sdk/jar/fonda-protocol-sdk-common-1.0.0.jar",
    "/Users/starlin/PycharmProjects/sdk/jar/fonda-protocol-sdk-f2-1.0-SNAPSHOT.jar",
    "/Users/starlin/PycharmProjects/sdk/jar/logback-core-1.2.12.jar",
]
jars = []

def start_jvm():
    for jar in jars_path:
        jars.append(jar)
    jpype.startJVM(classpath=jars)
    return True


def end_jvm():
    """
    关闭JAVA虚拟机
    :return:
    """
    jpype.shutdownJVM()

# 解析F2登陆指令数据
def f2_login_decode(byte_str):
    c_class = jpype.JClass('org.slf4j.impl.StaticLoggerBinder')
    F2ProtocolHelper = jpype.JClass('com.fonda.protocol.sdk.f2.codec.F2ProtocolHelper')

    byte_str = bytes.fromhex(byte_str)
    # 解析登陆数据,登陆不需要传入灯头数
    f2DeviceMessage = F2ProtocolHelper().decode(byte_str, None, True)  # 传入所需参数
    lamp_num = f2DeviceMessage.getLampNum()
    device_id = f2DeviceMessage.getDeviceId()
    print("lamp_num",lamp_num)
    print("device_id",device_id)
    return f2DeviceMessage

# 实时数据上报
def f2_data_report_decode(byte_str,lampNum):
    F2ProtocolHelper = JClass('com.fonda.protocol.sdk.f2.codec.F2ProtocolHelper')
    byte_str = bytes.fromhex(byte_str)

    LampNumGetterInterface = JClass('com.fonda.protocol.sdk.f2.codec.LampNumGetter')

    def get_lamp_num(deviceId):
        return lampNum

    lampNumGetter = JProxy(LampNumGetterInterface, dict(
        getLampNum=get_lamp_num
    ))

    result = F2ProtocolHelper.decode(byte_str, lampNumGetter, True)
    # 具体查看SDK文档
    # 返回设备Id ：result.getDeviceId()
    # 返回灯头数：result.getLampNum()
    # 协议封装对象：result.getProtocol()
    # 协议封装对象类：result.getProtocolClass()
    # 原始协议对象: result.getOriginalProtocol()
    # 原始协议对象数据: result.getOriginalProtocolData()
    return result

# 心跳
def f2_heartbeat_decode(byte_str,lampNum):
    F2ProtocolHelper = JClass('com.fonda.protocol.sdk.f2.codec.F2ProtocolHelper')
    byte_str = bytes.fromhex(byte_str)
    LampNumGetterInterface = JClass('com.fonda.protocol.sdk.f2.codec.LampNumGetter')

    def get_lamp_num(deviceId):
        return lampNum

    lampNumGetter = JProxy(LampNumGetterInterface, dict(
        getLampNum=get_lamp_num
    ))

    result = F2ProtocolHelper.decode(byte_str, lampNumGetter, True)
    # 具体查看SDK文档
    # 返回设备Id ：result.getDeviceId()
    # 返回灯头数：result.getLampNum()
    # 协议封装对象：result.getProtocol()
    # 协议封装对象类：result.getProtocolClass()
    # 原始协议对象: result.getOriginalProtocol()
    # 原始协议对象数据: result.getOriginalProtocolData()
    return result

if __name__ == '__main__':
    start_jvm()
    login_byte_str = '6800b40200000068016af220020009014c4355313531472834292d4538463256312e302e3132000000000000000000000938363030363130363438333037363400000000000b3839383630344234313032323430313736393238000000000000000000000000101107b95b1706070300340e34879016'
    data_report_byte_str = '6800b40200000068013cf212090000011e3c5d20720721eb100000230f04000022302580b80b5b1706070300340e15641cd41d00001df100000007b95700003600007e00FF8Bda16'
    heartbeat_byte_str = '6800b402000003680106f201a2ef01768b16'

    f2DeviceMessage = f2_login_decode(login_byte_str)
    f2_data_report_decode(data_report_byte_str,f2DeviceMessage.getLampNum())
    f2_heartbeat_decode(heartbeat_byte_str,f2DeviceMessage.getLampNum())
    f2_heartbeat_decode(heartbeat_byte_str)
    end_jvm()